import 'package:flutter/material.dart';

/// author：william on 2021/3/23 15:05
/// email：362661044@qq.com
class BaseLabelTableRow {
  final bool isRequired;
  final String text;
  final Widget label;
  final Widget child;
  final double height;

  /// 标题文本
  BaseLabelTableRow({this.height, @required this.text, @required this.child, this.isRequired = false})
      : label = Text(text),
        assert(text != null);

  /// 纯 widget
  BaseLabelTableRow.label({this.height, @required this.label, @required this.child, this.text, this.isRequired = false}) : assert(label != null);

  /// 纯文本
  BaseLabelTableRow.text({this.height, @required this.text, @required String childText, this.isRequired = false})
      : label = Text(text),
        child = Text(childText),
        assert(text != null),
        assert(childText != null);

  /// 重建
  BaseLabelTableRow.builder({@required this.label, @required Builder builder, this.text, this.isRequired = false})
      : height = null,
        child = builder,
        assert(label != null);
}

/// label 对齐 列表组件
// ignore: must_be_immutable
class BaseLabelTableContainer extends StatelessWidget {
  final List<BaseLabelTableRow> children;
  final double labelWidth;
  final double runSpacing;
  final bool showBorder;
  final Color runColor;
  final double defaultRowHeight;
  final TextStyle labelStyle;
  final bool enableBaseFormLabel;

  final TableCellVerticalAlignment defaultVerticalAlignment;

  BaseLabelTableContainer({
    Key key,
    @required this.children,
    this.labelWidth = 84,
    this.runSpacing = 0,
    this.showBorder = false,
    this.defaultRowHeight = 32,
    this.defaultVerticalAlignment,
    this.runColor,
    this.labelStyle,
    this.enableBaseFormLabel = false,
  }) : super(key: key);

  final double textHeight = 1.4;
  BuildContext _context;

  @override
  Widget build(BuildContext context) {
    _context = context;
    List<TableRow> _list = [];

    /// 创建 row
    for (int i = 0; i < children.length; i++) {
      _list.add(_buildRow(i, context));
      _list.add(_buildRunSpacing(i));
    }

    return Table(
      defaultVerticalAlignment: defaultVerticalAlignment ?? TableCellVerticalAlignment.baseline,
      textBaseline: TextBaseline.ideographic,
      columnWidths: {0: FixedColumnWidth(this.labelWidth)},
      children: _list,
    );
  }

  TableRow _buildRow(int index, BuildContext context) {
    BaseLabelTableRow model = this.children[index];
    return TableRow(
      decoration: BoxDecoration(color: runColor),
      children: [
        _buildLabel(model, context),
        DefaultTextStyle(
          style: Theme.of(_context).textTheme.bodyText2.copyWith(height: textHeight),
          child: _buildRowContent(model),
        ),
      ],
    );
  }

  Widget _buildRowContent(BaseLabelTableRow model) {
    if (model.child == null) return SizedBox();
    if (model.child is Builder) return model.child;
    return Container(
      alignment: _getAlignment(),
      height: model.height ?? defaultRowHeight,
      width: double.infinity,
      child: model.child,
    );
  }

  Alignment _getAlignment() {
    switch (defaultVerticalAlignment) {
      case TableCellVerticalAlignment.top:
        return Alignment.topLeft;
      case TableCellVerticalAlignment.middle:
        return Alignment.centerLeft;
      case TableCellVerticalAlignment.bottom:
        return Alignment.bottomLeft;
      case TableCellVerticalAlignment.baseline:
        return Alignment.centerLeft;
      case TableCellVerticalAlignment.fill:
        return Alignment.centerLeft;
    }
    return Alignment.centerLeft;
  }

  Widget _buildLabel(BaseLabelTableRow model, BuildContext context) {
    return enableBaseFormLabel
        ? _buildBaseFormLabel(context: context, isRequired: model.isRequired, text: model.text)
        : DefaultTextStyle(
            style: this.labelStyle ?? Theme.of(_context).textTheme.bodyText2,
            child: model.label,
          );
  }

  Widget _buildBaseFormLabel({@required BuildContext context, bool isRequired = false, @required String text, FontWeight fontWeight = FontWeight.normal}) {
    return RichText(
      text: TextSpan(children: [
        WidgetSpan(child: Text('* ', style: TextStyle(color: isRequired == true ? Colors.red : Colors.transparent))),
        TextSpan(text: text, style: Theme.of(context).textTheme.bodyText2.copyWith(fontWeight: fontWeight)),
      ]),
    );
  }

  TableRow _buildRunSpacing(int index) {
    double spacing = _isLastIndex(index) == false ? this.runSpacing : 0;
    BoxBorder _border = Border(
      bottom: BorderSide(color: Theme.of(_context)?.dividerColor, width: _isLastIndex(index) == false ? (Theme.of(_context).dividerTheme?.thickness ?? 0) : 0),
    );
    if (showBorder == false) _border = null;
    return TableRow(
      children: [
        ...List.generate(
          2,
          (index) => Container(
            alignment: Alignment.center,
            height: spacing,
            child: Container(height: 1, decoration: BoxDecoration(border: _border)),
          ),
        ),
      ],
    );
  }

  bool _isLastIndex(int index) {
    return index == (this.children.length - 1);
  }
}
